package com.eastjet.maintenance.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eastjet.maintenance.domain.*;
import com.eastjet.maintenance.feign.CodeDataFeing;
import com.eastjet.maintenance.mapper.EquipmentMapper;
import com.eastjet.maintenance.service.EquipmentService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author 杨博文
 * @description 针对表【equipment(设备档案)】的数据库操作Service实现
 * @createDate 2024-11-25 09:23:38
 */
@Service
public class EquipmentServiceImpl extends ServiceImpl<EquipmentMapper, Equipment> implements EquipmentService {

    @Autowired
    private EquipmentMapper equipmentMapper;

    @Autowired
    private AnnexServiceImpl annexService;//附件service
    @Autowired
    private SubDevicesServiceImpl subDevicesService;//子设备service
    @Autowired
    private SImageServiceImpl imageServiceImpl;//注入图片Service
    @Autowired
    private DocumentationServiceImpl documentationServiceImpl;//注入文档Service
    @Autowired
    private CodeDataFeing codeDataFeing;//自动生成代码

    /**
     * `InsertEquipment`方法的实现
     *
     * @param equipment
     * @return
     */
    @SneakyThrows
    @Override
    public Boolean InsertEquipment(Map<String, Object> map) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date datee = df.parse(df.format(new Date()));
        String code = codeDataFeing.test(new HashMap<String, String>() {{
            put("TableName", "equipment");
        }});
        Equipment equipment = new Equipment();
        equipment.setCode(code);
        equipment.setName(map.get("name").toString());
        equipment.setDeviceType(map.get("deviceType").toString());
        equipment.setSpecifications(map.get("specifications").toString());
        equipment.setManufacture(map.get("manufacture").toString());
        equipment.setManufacturer(map.get("manufacturer").toString());
        equipment.setFactoryNumber(map.get("factoryNumber").toString());
        equipment.setBrand(map.get("brand").toString());
        try {
            equipment.setDateofManufacture(df.parse(map.get("dateofManufacture").toString()));
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        equipment.setTotalPower(BigDecimal.valueOf(Double.parseDouble(map.get("totalPower").toString())));
        equipment.setWeight(BigDecimal.valueOf(Double.parseDouble(map.get("weight").toString())));
        equipment.setEquipmentUse(map.get("equipmentUse").toString());
        equipment.setStorageLocation((map.get("storageLocation").toString()));
        equipment.setCurrentStatus(Integer.valueOf(map.get("currentStatus").toString()));
        equipment.setUseDepartments(map.get("useDepartments").toString());
        equipment.setTheEquipment(map.get("theEquipment").toString());
        equipment.setContact(map.get("contact").toString());
        equipment.setOriginalValue(map.get("originalValue").toString());
        equipment.setCreatedBy(map.get("usname").toString());
        equipment.setCreationTime(df.parse(df.format(new Date())));
        equipment.setUpdater(map.get("usname").toString());
        equipment.setUpdated(df.parse(df.format(new Date())));
        try {
            equipment.setDateofEnablement(df.parse(map.get("dateofEnablement").toString()));
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        equipment.setInstallationSite(map.get("installationSite").toString());
        equipment.setRemark(map.get("remark").toString());
        boolean result = this.save(equipment);
        if (result) {
            // 保存附件
            List<Object> tableList = (List<Object>) map.get("attachments");
            List<Annex> annexList = new ArrayList<>();
            tableList.forEach(item -> {
                item = (Map<String, Object>) item;
                Annex annex = new Annex();
                annex.setCode(code);
                annex.setNumbering(code + "fjian");
                annex.setName(((Map<?, ?>) item).get("name").toString());
                annex.setModel(((Map<?, ?>) item).get("model").toString());
                annex.setType(((Map<?, ?>) item).get("type").toString());
                annex.setQuantity(Integer.valueOf(((Map<?, ?>) item).get("quantity").toString()));
                annex.setRemark(((Map<?, ?>) item).get("remark").toString());
                annexList.add(annex);
            });
            boolean b = annexService.saveBatch(annexList);
            if (b) {
                // 保存子设备
                List<Object> subDevicesList = (List<Object>) map.get("childDevices");
                List<SubDevices> subDevices = new ArrayList<>();
                subDevicesList.forEach(item -> {
                    item = (Map<String, Object>) item;
                    SubDevices subDevice = new SubDevices();
                    subDevice.setCode(code);
                    subDevice.setNumbering(((Map<?, ?>) item).get("numbering").toString());
                    subDevice.setName(((Map<?, ?>) item).get("name").toString());
                    subDevice.setModel(((Map<?, ?>) item).get("model").toString());
                    subDevice.setType(((Map<?, ?>) item).get("type").toString());
                    subDevice.setDepartment(((Map<?, ?>) item).get("department").toString());
                    subDevice.setLocation(((Map<?, ?>) item).get("location").toString());
                    subDevice.setState(Integer.valueOf(((Map<?, ?>) item).get("state").toString()));
                    subDevice.setRemark(((Map<?, ?>) item).get("remark").toString());
                    subDevices.add(subDevice);
                });
                boolean b1 = subDevicesService.saveBatch(subDevices);
                if (b1) {
                    //新增图片
                    List<Object> imageData = (List<Object>) map.get("fileList2");
                    List<SImage> imageList = new ArrayList<>();
                    //循环映射图片数据
                    imageData.forEach(item -> {
                        item = (Map<String, Object>) item;
                        SImage img = new SImage();
                        img.setCode(code);
                        img.setEid(Integer.valueOf(((Map<?, ?>) item).get("size").toString()));
                        img.setPath(((Map<?, ?>) item).get("name").toString());
                        img.setCreationTime(datee);
                        img.setCreatedBy(map.get("usname").toString());
                        imageList.add(img);
                    });
                    Boolean b2 = imageServiceImpl.InsertImage(imageList);
                    if (b2) {
                        //新增文档
                        List<Object> documentationData = (List<Object>) map.get("documents");
                        List<Documentation> documentationList = new ArrayList<>();
                        //循环映射文档数据
                        documentationData.forEach(item -> {
                            item = (Map<String, Object>) item;
                            Documentation doc = new Documentation();
                            doc.setCode(code);
                            doc.setFilename(((Map<?, ?>) item).get("name").toString());
                            doc.setUploadTime(datee);
                            doc.setUploader(map.get("usname").toString());
                            doc.setSize(((Map<?, ?>) item).get("size").toString());
                            doc.setEid(1);
                            doc.setCreationTime(datee);
                            doc.setCreatedBy(map.get("usname").toString());
                            doc.setTombstone(0);
                            documentationList.add(doc);
                        });
                        return documentationServiceImpl.InsertList(documentationList);
                    }
                }
            }
        }

        return false;
    }

    /**
     * 查询所有设备信息/条件查询
     *
     * @param equipment
     * @return
     */
    @Override
    public List<Equipment> SelectAllEquipment(Equipment equipment) {
        return equipmentMapper.SelectEquipment(equipment);
    }

    /**
     * 删除设备信息
     *
     * @param code
     * @return
     */
    @Override
    public Boolean DeleteEquipment(String code) {
        return equipmentMapper.DeleteEquipment(code) == 1 ? true : false;
    }

    /**
     * 更新设备信息
     *
     * @param equipment
     * @return
     */
    @Override
    public Boolean UpdateEquipment(Equipment equipment) {
        return this.updateById(equipment);
    }


}




